//
//  ZBBRadioViewController.m
//  沙哈拉FM
//
//  Created by dllo on 15/11/12.
//  Copyright © 2015年 S1mpl3. All rights reserved.
//

#import "ZBBRadioViewController.h"
#import "FitHeader.h"
#import "UIImageView+WebCache.h"
#import "BaseButton.h"
#import "BaseLabel.h"
#import "LZBillboardModel.h"
#import "UIImage+GaussianBlur.h"
#import "TwoToOne.h"
#import "TabBarController.h"
@interface ZBBRadioViewController ()
@property(nonatomic, strong)BaseLabel *titleLabel;
@property(nonatomic, strong)BaseLabel *playtimesLabel;
@property(nonatomic, strong)BaseButton *button;
@property(nonatomic, strong)UIImageView *coverMiddleImageView;
@property(nonatomic, strong)UIImageView *listImageView;
@property(nonatomic, strong)UIImageView *goBackImageView;
@property(nonatomic, strong)UIImageView *playOrPauseImageView;
@property(nonatomic, strong)UIImageView *goNextImageView;
@property(nonatomic, strong)UIImageView *historyImageView;
@property(nonatomic, strong)UIImageView *backImageView;
@property(nonatomic, strong)UINavigationController *nav1;
@property(nonatomic, strong)BaseButton *downloadButton;
@property(nonatomic, strong)BaseButton *shareButton;
@property(nonatomic, assign)NSInteger flag;
@property(nonatomic, strong)LZBillboardModel *lzBill;
@property(nonatomic, assign)NSInteger index;
@property(nonatomic, strong)NSMutableArray *dataArray;

@property(nonatomic, strong)NSArray *dicArray;
@property(nonatomic, strong)NSMutableArray *dataSourceArray;
@property(nonatomic, strong)NSTimer *timer;
@property(nonatomic, strong)NSString *urlString;
@property(nonatomic, strong)AVPlayer *player;
@property(nonatomic, strong)UIProgressView *progressView;//播放进度条
@property(nonatomic, strong)BaseLabel *LZlabel;
@property(nonatomic, strong)id progressViewObserver;//播放进度条观察者
@property(nonatomic, strong)NSDictionary *historyDic;
@property(nonatomic, strong)UISlider *slider;
@property(nonatomic, strong)UIView *backView;
@property(nonatomic, assign)CGFloat totalTime;
@property(nonatomic, assign)CGFloat currentTime;
@property(nonatomic, strong)UIImageView *rectView;
@property(nonatomic, strong)UIImageView *myImageView;
@property(nonatomic, strong)UILabel *nameLabel;
@property(nonatomic, strong)UILabel *hostLabel;
@property(nonatomic, strong)UILabel *timeLabel;
@property(nonatomic, strong)UIImageView *downImageView;
@property(nonatomic, strong)UILabel *downTitleLabel;
@property(nonatomic, strong)UILabel *downPronameLabel;
@property(nonatomic, strong)UILabel *playCountLabel;
@end

@implementation ZBBRadioViewController

- (void)viewWillAppear:(BOOL)animated{
    self.navigationController.navigationBar.tintColor = [UIColor clearColor];
    self.navigationController.navigationBarHidden = YES;
    [[TabBarController shareInstance] totalTabBarShouldHiden:YES];
    
    
    if ([[NSUserDefaults standardUserDefaults] integerForKey:@"cell0"] == 1) {
        
          [self getZBBData];
        
        _titleLabel.text = [_dic objectForKey:@"rname"];
        [[LZMusicSingleton shareInstance] play:_dic];
        [[TabBarController shareInstance] totalTabBarShouldHiden:YES];
        _downTitleLabel.text = [_dic objectForKey:@"rname"];
        [TabBarController shareInstance].flagInit = 1;
        [TabBarController shareInstance].playButton.alpha = 0;
        [TabBarController shareInstance].dic = _dic;
        [TabBarController shareInstance].nav = self.navigationController;
        _downPronameLabel.text =  [NSString stringWithFormat:@"正在播出:%@",[_dic objectForKey:@"programName"]];
        _downPronameLabel.textColor = [UIColor grayColor];
        _downPronameLabel.font = [UIFont systemFontOfSize:15];

        //NSLog(@"%@", _dic);

        

        
        if ([_dic objectForKey:@"picPath"]) {
            [_downImageView sd_setImageWithURL:[NSURL URLWithString:[_dic objectForKey:@"picPath"]]];
            
            [[TabBarController shareInstance].imageView sd_setImageWithURL:[NSURL URLWithString:[_dic objectForKey:@"picPath"]]];
            _backImageView.image = [UIImage imageWithBlurImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[_dic objectForKey:@"picPath"]]]] intputRadius:10];
            
        }
        else
        {
            [_downImageView sd_setImageWithURL:[NSURL URLWithString:[_dic objectForKey:@"radioCoverLarge"]]];
            [[TabBarController shareInstance].imageView sd_setImageWithURL:[NSURL URLWithString:[_dic objectForKey:@"radioCoverLarge"]]];
            _backImageView.image = [UIImage imageWithBlurImage:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[_dic objectForKey:@"radioCoverLarge"]]]] intputRadius:10];
            
        }
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [[self navigationController] setNavigationBarHidden:YES animated:NO];
    self.view.backgroundColor = [UIColor whiteColor];
    _backImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 540 * FITHeight)];
    
    _backView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 540 * FITHeight)];
    [_backImageView addSubview:_backView];
    [self.view addSubview:_backImageView];
    _button = [BaseButton buttonWithType:UIButtonTypeSystem];
    _button.frame = CGRectMake(20 * FITWidth, 30 * FITHeight, 30 * FITWidth, 30 * FITHeight);
    [_button setTintColor:[UIColor whiteColor]];
    [_button setImage:[UIImage imageNamed:@"下箭头"] forState:UIControlStateNormal];
    [_button addTarget:self action:@selector(goback) forControlEvents:UIControlEventTouchUpInside];
    [_backView addSubview:_button];
    
    
    
    _titleLabel = [[BaseLabel alloc]initWithFrame:CGRectMake(_button.frame.origin.x + _button.frame.size.width + 10 * FITWidth, _button.frame.origin.y, 250 * FITWidth, _button.frame.size.height)];
    _titleLabel.textColor = [UIColor whiteColor];
    _titleLabel.textAlignment = 1;
    _titleLabel.font = [UIFont systemFontOfSize:17];
    [_backView addSubview:_titleLabel];
    
    
    
    
    _coverMiddleImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_titleLabel.frame.origin.x + 20 * FITWidth, _titleLabel.frame.origin.y + _titleLabel.frame.size.height + 80 * FITHeight, 215 * FITWidth, 215 * FITHeight)];
    [_coverMiddleImageView sd_setImageWithURL:[NSURL URLWithString:_lzBill.coverMiddle]];
    _coverMiddleImageView.layer.cornerRadius = (215 / 2) * FITWidth;
    _coverMiddleImageView.clipsToBounds = YES;
    [_backView addSubview:_coverMiddleImageView];
    
    _progressView = [[UIProgressView alloc]initWithProgressViewStyle:UIProgressViewStyleDefault];
    _progressView.frame = CGRectMake(0, 540 * FITHeight, self.view.frame.size.width, 20);
    [_backView addSubview:_progressView];
    
    _LZlabel = [[BaseLabel alloc] initWithFrame:CGRectMake(_progressView.frame.origin.x + _progressView.frame.size.width + 3 * FITWidth, _progressView.frame.origin.y - 10 * FITHeight, 60 * FITWidth, 20 * FITHeight)];
    [_LZlabel setFontSize:10];
    _LZlabel.textColor = [UIColor lightGrayColor];
    [_backView addSubview:_LZlabel];
    
    _listImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_button.frame.origin.x, _coverMiddleImageView.frame.origin.y + _coverMiddleImageView.frame.size.height + 110 * FITHeight, 32 * FITWidth, 32 * FITHeight)];
    //_listImageView.image = [UIImage imageNamed:@"播放列表"];
    _listImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *listTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(listAction)];
    [_listImageView addGestureRecognizer:listTap];
    [_backView addSubview:_listImageView];
    
    _goBackImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_coverMiddleImageView.frame.origin.x + 20 * FITWidth, _listImageView.frame.origin.y, _listImageView.frame.size.width, _listImageView.frame.size.height)];
    _goBackImageView.image = [UIImage imageNamed:@"上一首"];
    _goBackImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *goBackTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(goBackAction)];
    [_goBackImageView addGestureRecognizer:goBackTap];
    
    [_backView addSubview:_goBackImageView];
    
    _playOrPauseImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_coverMiddleImageView.frame.origin.x + 85 * FITWidth, _goBackImageView.frame.origin.y - 10 * FITHeight, 48 * FITWidth, 48 * FITHeight)];
    _playOrPauseImageView.image = [UIImage imageNamed:@"暂停白色"];
    _playOrPauseImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(playOrPauseAction)];
    [_playOrPauseImageView addGestureRecognizer:tap];
    [_backView addSubview:_playOrPauseImageView];
    
    _goNextImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_coverMiddleImageView.frame.origin.x + _coverMiddleImageView.frame.size.width - 52 * FITWidth, _goBackImageView.frame.origin.y, _goBackImageView.frame.size.width, _goBackImageView.frame.size.height)];
    _goNextImageView.image = [UIImage imageNamed:@"下一首"];
    _goNextImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *goNextTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(goNextAction)];
    [_goNextImageView addGestureRecognizer:goNextTap];
    [_backView addSubview:_goNextImageView];
    
    _historyImageView = [[UIImageView alloc]initWithFrame:CGRectMake( _goNextImageView.frame.origin.x + _goNextImageView .frame.size.width + 40 * FITWidth, _goBackImageView.frame.origin.y, _listImageView.frame.size.width, _listImageView.frame.size.height)];
    _historyImageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *historyTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(historyAction)];
    [_historyImageView addGestureRecognizer:historyTap];
   // _historyImageView.image = [UIImage imageNamed:@"播放历史"];
    [_backView addSubview:_historyImageView];
    [_backView addSubview:self.rectView];
    [_backView addSubview:self.myImageView];
    _downImageView = [[UIImageView alloc]initWithFrame:CGRectMake(_backView.frame.origin.x + 5 * FITWidth, _backView.frame.origin.y + _backView.frame.size.height + 10 * FITHeight, 90 * FITWidth, 100 * FITHeight)];
    [self.view addSubview:_downImageView];
    _downTitleLabel = [[UILabel alloc]initWithFrame:CGRectMake(_downImageView.frame.origin.x + _downImageView.frame.size.width + 5 * FITWidth, _downImageView.frame.origin.y, 120 * FITWidth, 30 * FITHeight)];
    
    [self.view addSubview:_downTitleLabel];
    _downPronameLabel = [[UILabel alloc]initWithFrame:CGRectMake(_downImageView.frame.origin.x + _downImageView.frame.size.width + 5 * FITWidth, _downTitleLabel.frame.origin.y + _downTitleLabel.frame.size.height + 5 * FITHeight, 200 * FITWidth, 30 * FITHeight)];
    
    [self.view addSubview:_downPronameLabel];
    
    self.myImageView.image = [UIImage imageNamed:@"zbbradio.png"];
    _backView.backgroundColor = [UIColor colorWithRed:0.33 green:0.33 blue:0.38 alpha:0.8];
    _backImageView.userInteractionEnabled = YES;
    _backView.userInteractionEnabled = YES;
    
    
    
    
    
    
}
-(UIImageView*)rectView
{
    if (_rectView == nil) {
        _rectView = [[UIImageView alloc]initWithFrame:CGRectMake(30 * FITWidth, 80 * FITHeight, 300 * FITWidth, 300 * FITHeight)];
        _rectView.layer.borderWidth = 1.5;
        _rectView.layer.borderColor = [[UIColor colorWithRed:0.71 green:0.71 blue:0.71 alpha:1] CGColor];
        
        
        
    }
    return _rectView;
}

-(UIImageView *)myImageView
{
    if (_myImageView == nil) {
        _myImageView = [[UIImageView alloc]initWithFrame:CGRectMake(30 * FITWidth, 380 * FITHeight, 300 * FITWidth, 40 * FITHeight)];
        _myImageView.layer.borderWidth = 1.5;
        _myImageView.layer.borderColor = [[UIColor colorWithRed:0.4 green:0.4 blue:0.45 alpha:1] CGColor];
    }
    return _myImageView;
}
-(UILabel *)nameLabel
{
    if (_nameLabel == nil) {
        _nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(60 * FITWidth, 100 * FITHeight, 200 * FITWidth, 40 * FITHeight)];
        _nameLabel.textColor = [UIColor whiteColor];
        _nameLabel.font = [UIFont systemFontOfSize:25];
        
        [self.rectView addSubview:_nameLabel];
        _nameLabel.textAlignment = 1;
    }
    return _nameLabel;
}
-(UILabel *)hostLabel
{
    if (_hostLabel == nil) {
        _hostLabel = [[UILabel alloc]initWithFrame:CGRectMake(60 * FITWidth, 165 * FITHeight, 200 * FITWidth, 20 * FITHeight)];
        _hostLabel.textColor = [UIColor whiteColor];
        _hostLabel.font = [UIFont systemFontOfSize:15];
    }
    _hostLabel.textAlignment = 1;
    [self.rectView addSubview:_hostLabel];
    return _hostLabel;
}
-(UILabel *)timeLabel
{
    if (_timeLabel == nil) {
        _timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(60 * FITWidth, 190 * FITHeight, 200 * FITWidth, 20 * FITHeight)];
        _timeLabel.textColor = [UIColor whiteColor];
        _timeLabel.font = [UIFont systemFontOfSize:15];
    }
    _timeLabel.textAlignment = 1;
    [self.rectView addSubview:_timeLabel];
    return _timeLabel;
}

-(void)getZBBData
{
    _dataSourceArray  = [NSMutableArray array];
    _urlString = [NSString stringWithFormat:@"http://live.ximalaya.com/live-web/v1/getProgramSchedules?device=android&radioId=%@", [_dic objectForKey:@"radioId"]];
    [TwoToOne getDataByString:_urlString BodyString:nil WithDataBlock:^(id data) {
        
        
        NSDictionary *dictionary = [data objectForKey:@"result"];
        NSArray *array = [dictionary objectForKey:@"todaySchedules"];
        _dicArray = array;
        for (NSDictionary *dic in array) {
            
            LZBillboardModel *lzBil = [[LZBillboardModel alloc]init];
            [lzBil setValuesForKeysWithDictionary:dic];
            [_dataSourceArray addObject:lzBil];
        }
        
        
        for (NSInteger i = 0; i < array.count; i++)
        {
            if ([[_dic objectForKey:@"programScheduleId"] integerValue]
                == [[array[i] objectForKey:@"programScheduleId"] integerValue]) {
                _index = i;
                
                break;
            }
            
        }
        [[LZMusicSingleton shareInstance] ZbbPlay:
         _dicArray[_index]];
        [self  relodata];
        
    }];
    
    
}
-(void)relodata
{
    self.nameLabel.text = [_dataSourceArray[_index] programName];
    if ([_dataSourceArray[_index] announcerList].count != 0) {
        
        self.hostLabel.text = [ [_dataSourceArray[_index] announcerList][0] objectForKey:@"announcerName"];
        
    }
    
    
    self.timeLabel.text = [_dataSourceArray[_index] endTime];
    
}

-(void)goback
{
    [[TabBarController shareInstance] AudioWillPlay:[[NSUserDefaults standardUserDefaults] integerForKey:@"play"]];
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"cell0"];
    [self.navigationController popViewControllerAnimated:YES];
}
//播放列表
-(void)listAction
{
    
}
//上一首
-(void)goBackAction
{
    _index--;
    if (_index == 0) {
        _goBackImageView.image = [UIImage imageNamed:@"上一首灰色"];
        _goBackImageView.userInteractionEnabled = NO;
        [[LZMusicSingleton shareInstance] play:_dicArray[_index]];
    }
    
    _playOrPauseImageView.image = [UIImage imageNamed:@"暂停白色"];
    _totalTime = [[_dic objectForKey:@"duration"]floatValue];
    _currentTime = 0;
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"play"];
    
    if (_index > 0) {
        _goBackImageView.userInteractionEnabled = YES;
        _goBackImageView.image = [UIImage imageNamed:@"上一首"];
        _playOrPauseImageView.image = [UIImage imageNamed:@"暂停白色"];
        
        [[LZMusicSingleton shareInstance] ZbbPlay:_dicArray[_index]];
        
        
        _currentTime = 0;
        
        
        
    }
    
    [self relodata];
}
//播放
-(void)playOrPauseAction
{
    if ([[NSUserDefaults standardUserDefaults] integerForKey:@"play"] == 0) {
        _playOrPauseImageView.image = [UIImage imageNamed:@"播放白色"];
        [[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"play"];
        


        
        //[_coverMiddleImageView.layer removeAllAnimations];
        

        [[LZMusicSingleton shareInstance].player pause];
    }else{
        [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"play"];
        _playOrPauseImageView.image = [UIImage imageNamed:@"暂停白色"];
        
        [[LZMusicSingleton shareInstance].player play];
    }
    [[TabBarController shareInstance] AudioWillPlay:[[NSUserDefaults standardUserDefaults] integerForKey:@"play"]];
    
    [self relodata];
}
//下一首
-(void)goNextAction
{
    
    _index++;
    if (_index == _dicArray.count) {
        _goNextImageView.image = [UIImage imageNamed:@"下一首白色"];
        _goNextImageView.userInteractionEnabled = NO;
        _playOrPauseImageView.image = [UIImage imageNamed:@"暂停白色"];
        _currentTime = 0;
        [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"play"];
        
        
        return;
    }
    if (_index < _dicArray.count) {
        
        [[LZMusicSingleton shareInstance] ZbbPlay:_dicArray[_index]];
        
        _goNextImageView.userInteractionEnabled = YES;
        _goNextImageView.image = [UIImage imageNamed:@"下一首"];
        _playOrPauseImageView.image = [UIImage imageNamed:@"暂停白色"];
        
        _currentTime = 0;
        [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"play"];
        
        
    }
    
    [self relodata];
}
//播放历史
-(void)historyAction
{
    
}


-(void)viewDidDisappear:(BOOL)animated
{
    [[TabBarController shareInstance] totalTabBarShouldHiden:NO];
    
    
}
-(void)viewWillDisappear:(BOOL)animated
{
    [[self navigationController] setNavigationBarHidden:NO animated:NO];
    
}
+(ZBBRadioViewController *)shareInstance{
    static ZBBRadioViewController *zbbRadio = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        zbbRadio = [[ZBBRadioViewController alloc]init];
    });
    return zbbRadio;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
